import pymysql

# 打开数据库连接

conn = pymysql.connect(host="139.9.219.187",port=3306,user="lh",passwd="159753",db="movie_big_data",charset='utf8')
# 使用cursor()方法获取操作游标
cursor = conn.cursor()
#批量插入数据
try:
    sql_gettype = "select id,type from movie_info"
    cursor.execute(sql_gettype)
    result = cursor.fetchall()
    list_type = []
    for data in result:
        type = data[1].split(' ')
        for i in type:
            list_type.append((data[0],i))

    sql_insert_type = 'insert into  movie_info_type (movie_id,type) values(%s,%s)'
    cursor.executemany(sql_insert_type, list_type)
    conn.commit()

except Exception as e:
    print(e)
    conn.rollback()
#关闭
finally:
    conn.close()
    cursor.close()
